import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home'
import Users from '../components/users/Users'
import Brand from '../components/brand/Brand'
import Addbrand from '../components/brand/Addbrand'
import Recyclebin from '../components/brand/Recyclebin'
import Cate from '../components/cate/Cate'
import Addcate from '../components/cate/Addcate'
import Goods from '../components/goods/Goods'
import AddGood from '../components/goods/AddGood'
import Roles from '../components/power/Roles'
import Right from '../components/power/Right'
import Order from '../components/orders/Order'
import Welcome from '../components/Welcome'
import Login from '../components/Login'
Vue.use(VueRouter)

const routes = [
  {
    path: '/home', component: Home,
    children:[
      {path:'/users', component:Users},
      {path:'/brand', component:Brand},
      {path:'/addbrand', component:Addbrand},
      {path:'/recyclebin', component:Recyclebin},
      {path:'/cate', component:Cate},
      {path:'/addcate', component:Addcate},
      {path:'/goods', component:Goods},
      {path:'/role', component:Roles},
      {path:'/right', component:Right},
      {path:'/order', component:Order},
      {path: '/welcome', component:Welcome},
      {path: '/addgood', component:AddGood}
    ]
  },
  {path:'/', redirect:'/welcome'},
  {path:'/login', component: Login}
]


const router = new VueRouter({
  routes
})

router.beforeEach((to, from , next) => {
  if (to.path === '/login') return next()
  let token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
